<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">客户编号</label>
        <el-input v-model="query.lstCustNo" clearable placeholder="客户编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">客户姓名</label>
        <el-input v-model="query.lstCustName" clearable placeholder="客户姓名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">客户经理姓名</label>
        <el-input v-model="query.lstCustManagerName" clearable placeholder="客户经理姓名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">状态</label>
        <el-input v-model="query.lstStatus" clearable placeholder="状态" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item v-show="false" v-if="form.lstStatus==2" prop="lstLostDate" label="确认流失时间">
            <template>
              {{ form.lstLostDate=new Date() }}
              <!-- <el-date-picker v-model="form.lstLostDate" type="datetime" style="width: 370px;" /> -->
            </template>

          </el-form-item>
          <el-form-item v-if="form.lstStatus==2" prop="lstReason" label="流失原因">
            <el-input v-model="form.lstReason" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="状态">
            <el-select v-model="form.lstStatus" filterable placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.label"
                :value="item.value"
                :disabled="item.disabled"
              />
            </el-select>
          </el-form-item>

          <el-form-item prop="lstDelay" label="暂缓措施">
            <el-input v-model="form.lstDelay" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="lstId" label="编号" />
        <el-table-column prop="lstCustNo" label="客户编号">
          <!-- <template slot-scope="scope">
            {{ scope.row.lstCustNo }}
            {{ scope.row.lstId }}
          </template> -->
        </el-table-column>
        <el-table-column prop="lstCustName" label="客户姓名" />
        <el-table-column prop="lstCustManagerId" label="客户经理编号" />
        <el-table-column prop="lstCustManagerName" label="客户经理姓名" />
        <el-table-column prop="lstLastOrderDate" label="最后下单时间" />
        <el-table-column prop="lstLostDate" label="确认流失时间" />
        <el-table-column prop="lstDelay" label="暂缓措施" />
        <el-table-column prop="lstReason" label="流失原因" />
        <el-table-column prop="lstStatus" label="状态">
          <template slot-scope="scope">
            <div v-for="(item, index) in options" :key="index">
              <el-tag v-if="scope.row.lstStatus == item.value" :type="item.color">{{ item.label }}</el-tag>
            </div>
            <!--        {{ dict.label.cust_status[scope.row.lstStatus] }} -->
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','cstLost:edit','cstLost:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCstLost from '@/api/cstLost'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { lstId: null, lstCustNo: null, lstCustName: null, lstCustManagerId: null, lstCustManagerName: null, lstLastOrderDate: null, lstLostDate: null, lstDelay: null, lstReason: null, lstStatus: null }
export default {
  name: 'CstLost1',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['cust_status'],
  cruds() {
    return CRUD({ title: '客户流失管理', url: 'api/cstLost', idField: 'lstId', sort: 'lstId,desc', crudMethod: { ...crudCstLost }})
  },
  data() {
    return {
      options: [
        {
          value: '1',
          label: '恢复正常',
          color: 'success',
          disabled: true
        },
        {
          value: '2',
          label: '流失',
          color: 'danger'
        },
        {
          value: '3',
          label: '删除',
          color: 'info',
          disabled: true
        }, {
          value: '4',
          color: 'warning',
          label: '流失警告'
        },
        {
          value: '5',
          color: 'danger',
          label: '申请流失'
        }],
      permission: {
        add: ['admin', 'cstLost:add'],
        edit: ['admin', 'cstLost:edit'],
        del: ['admin', 'cstLost:del']
      },
      rules: {
        lstDelay: [
          { required: true, message: '暂缓措施不能为空', trigger: 'blur' }
        ],
        lstLostDate: [
          { required: true, message: '流失时间不能为空', trigger: 'blur' }
        ],
        lstReason: [
          { required: true, message: '流失原因不能为空', trigger: 'blur' }
        ],
        lstCustManagerId: [
          { required: true, message: '客户经理编号不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'lstCustNo', display_name: '客户编号' },
        { key: 'lstCustName', display_name: '客户姓名' },
        { key: 'lstCustManagerName', display_name: '客户经理姓名' },
        { key: 'lstStatus', display_name: '状态' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>
.ff{
  color: green;
}
</style>
